package com.compty.paint.freemarker;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import com.compty.core.framework.context.spring.SpringContextHolder;
import com.compty.core.page.Page;
import com.compty.paint.domain.Advertisement;
import com.compty.paint.domain.Bid;
import com.compty.paint.domain.CustomerInfo;
import com.compty.paint.domain.Goods;
import com.compty.paint.domain.Painter;
import com.compty.paint.domain.Painting;
import com.compty.paint.domain.SpecialSubject;
import com.compty.paint.freemarker.support.NavigateBean;
import com.compty.paint.manager.AdvertisementManager;
import com.compty.paint.manager.BidManager;
import com.compty.paint.manager.CustomerInfoManager;
import com.compty.paint.manager.GoodsManager;
import com.compty.paint.manager.PainterManager;
import com.compty.paint.manager.PaintingManager;
import com.compty.paint.manager.SpecialSubjectManager;

import freemarker.template.TemplateException;

/**
 * 瓷艺频道 
 */
public class CiyiGenerate extends BaseGenerate{

	private PaintingManager paintingManager;
    private PainterManager painterManager;
    private BidManager bidManager;
    private AdvertisementManager advertisementManager;
    private GoodsManager goodsManager;
    private CustomerInfoManager customerInfoManager;
    private SpecialSubjectManager specialSubjectManager;
    private Date startDate;
    
	public CiyiGenerate(Date startDate) {
		super();
		this.paintingManager = SpringContextHolder.getBean("paintingManager");
		this.painterManager =  SpringContextHolder.getBean("painterManager");
		this.bidManager = SpringContextHolder.getBean("bidManager");
		this.advertisementManager = SpringContextHolder.getBean("advertisementManager");
		this.goodsManager = SpringContextHolder.getBean("goodsManager");
		this.customerInfoManager = SpringContextHolder.getBean("customerInfoManager");
		this.specialSubjectManager = SpringContextHolder.getBean("specialSubjectManager");
		this.startDate = startDate;
	}

	public void generateHtml() throws TemplateException, IOException {
		
//		generatePaintingList();//生成作品列表页
		generatePainterDetail();//生成名家详情页及作品详情页
//		generatePaintingOfPainter();//生成作品详情页
	}
	
	@SuppressWarnings("unchecked")
	protected void generatePaintingList() throws TemplateException, IOException {
		
		int pageSize = 24;
		
		Page page  = advertisementManager.findByPage(curPage,3,"2");//瓷器频道广告
		List<Advertisement> advertisementList = (List<Advertisement>) page.getResult();
		root.put("advertisementList",advertisementList);
		
    	page = paintingManager.findCiYiByPage(1,12,"1");//名师作品
    	List<Painting>  mszp = (List<Painting>)page.getResult(); 
    	root.put("mszp", mszp);
    	page = paintingManager.findCiYiByPage(1,12,"2");//高仿精品
    	List<Painting>  gfjp = (List<Painting>)page.getResult(); 
    	root.put("gfjp", gfjp);
    	
		String prefix = "index";
		String templateHtml = "ciyi_index.ftl";
		generateHtmlByType(null,page, prefix, templateHtml,pageSize);
		
		Page page1 = paintingManager.findCiYiByPage(1,12,"1");//名师
		prefix = "ms";
		String type = "1";
		generateHtmlByType(type,page1, prefix, "ciyi_list.ftl",pageSize);

		Page page2 = paintingManager.findCiYiByPage(1,12,"2");//高仿
		prefix = "gf";
		type = "2";
		generateHtmlByType(type,page2, prefix, "ciyi_list.ftl",pageSize);
	}
	
	/**
	 * 生成画家详情页
	 * @throws IOException 
	 * @throws TemplateException 
	 */
	@SuppressWarnings("unchecked")
	private void generatePainterDetail() throws TemplateException, IOException{
		
		Page page = painterManager.queryPaintersByNameAndCatalogtype(1,20,null,"3");
		int pageCount = page.getPageCount();
		
		for (int i = 1; i <= pageCount; i++) {
			page = painterManager.queryPaintersByNameAndCatalogtype(i,20,null,"3");
			List<Painter> painters = (List<Painter>) page.getResult();
			for (Painter painter : painters) {
				SpecialSubject special = specialSubjectManager.findByPainterId(painter.getId());
				root.put("specialSubject", special);
				root.put("painter", painter);
				
				List<Painting> paintingList = paintingManager.findAvailablePaintingsByPainter(painter.getId());
				root.put("paintingList", paintingList);
				
				output("detail_painter.ftl", "",painter.getEnglishName()+".html");
			}
		}
	}
	
	@SuppressWarnings("unchecked")
	protected void generatePaintingOfPainter()  throws TemplateException, IOException{
		
		Page page = paintingManager.findCiYiByPage(1,20,null);
		int pageCount = page.getPageCount();
		for (int j =  1; j <= pageCount; j++) {
			page = paintingManager.findCiYiByPage(j,20,null);
			List<Painting>  paintings= (List<Painting>) page.getResult();
			for (Painting painting : paintings) {
				root.put("painting",painting);
				dealNavigateUrl(painting);
				
				/******************** 商品  begin  ***********************/
				Goods goods = goodsManager.findByPaintingId(painting.getId());
				if( goods != null ){
					root.put("goods", goods);
				}
				/******************** 商品  end  ***********************/
				
				/******************** 拍品  begin  ***********************/
				Bid bid = bidManager.findByPaintingId(painting.getId());
				if( bid != null ){
					bid = bidManager.findById(bid.getId());
					if(bid.getCurrentBidRecord() != null){
						if(bid.getCurrentBidRecord().getCustomerInfo() != null){
							Long c_id =  bid.getCurrentBidRecord().getCustomerInfo().getId();
							CustomerInfo customerInfo = customerInfoManager.get(c_id);
							bid.getCurrentBidRecord().setCustomerInfo(customerInfo);
						}
					}
					
					Date  endTime = bid.getEndTime();
					if(Calendar.getInstance().getTime().compareTo(endTime) < 0){
						bid.setNewFlag("1");
					}else{
						bid.setNewFlag("0");
					}
					root.put("bid", bid);
				}
				/******************** 拍品  end  ***********************/
				
				/******************** 是否显示预约信息 begin  ***********************/
				String onSaleFlag = painting.getOnSaleFlag();
				if("2".equals(onSaleFlag)){//表示此商品上架销售
					if( painting.getPainter() != null ){
						String remark = painting.getRemark();
						if("1".equals(remark)){
							Float price = painting.getPrice();
							if( price != null && price >= 15000){
								root.put("subscribeFlag", "1");
							}
						}
					}
				}
				/******************** 是否显示预约信息 end  *************************/
				
				getImagesArray(painting);
				
				Long  catalog_id = painting.getCatalogType().getId();
				List list = paintingManager.findRelativePaintingList(painting.getId(),8,catalog_id);
				root.put("relativeList", list);
				
				if("1".equals(painting.getOnSaleFlag()) && bid != null){
					if(painting.getPainter()!= null && painting.getPainter().getEnglishName() != null){
						output("detail_bid.ftl", "/ciyi/"+painting.getPainter().getEnglishName(), painting.getId()+".html");
					}else{
						output("detail_bid.ftl", "/ciyi", painting.getId()+".html");
					}
				}else{
					if(painting.getPainter()!= null && painting.getPainter().getEnglishName() != null){
						output("detail_painting.ftl", "/ciyi/"+painting.getPainter().getEnglishName(), painting.getId()+".html");
					}else{
						output("detail_painting.ftl", "/ciyi", painting.getId()+".html");
					}
				}
			}
		}
	}
	
	/**
	 * 生成列表页
	 */
	public void generateHtmlByType(String type,Page page,String prefix,String templateHtml,int pageSize) throws TemplateException, IOException{
		int pageCount = page.getPageCount();
		
		List<NavigateBean> navigateList = buildNavigateList(pageCount,prefix);
		
		for (int i = 1; i <= pageCount; i++) {
			 page = paintingManager.findCiYiByPage(i,pageSize,type);
			 root.put("page", page);
			 NavigateBean navigateBean = buildNavigateBean(prefix, i,pageCount,navigateList);
			 root.put("navigateBean", navigateBean);
			 if( i == 1){
				 output(templateHtml, "/ciyi", prefix+".html");
			 }
			 else{
				 output(templateHtml, "/ciyi", prefix+"_"+i+".html");
			 }
		}
	}
	
	private NavigateBean buildNavigateBean(String prefix, int curPage,int pageCount,List<NavigateBean> navigateBeanList) {
		NavigateBean navigateBean = new NavigateBean();
		navigateBean.setTitle("第"+curPage+"页");
		if(curPage ==1){
			navigateBean.setLinkUrl(prefix+".html");
		}else{
			navigateBean.setLinkUrl(prefix+"_"+curPage+".html");
		}
		
		String pre_linkurl = "";
		String next_linkurl = "";
		if(curPage > 1){
			pre_linkurl = prefix+"_"+(curPage-1)+".html";
		}
		if(curPage < pageCount ){
			next_linkurl = prefix+"_"+(curPage+1)+".html";
		}
		
		navigateBean.setPre_linkurl(pre_linkurl);
		navigateBean.setNext_linkurl(next_linkurl);
		
		navigateBean.setNavigateBeanList(navigateBeanList);
		
		return navigateBean;
	}

	private List<NavigateBean> buildNavigateList(int pageCount,String prefix) {
		List<NavigateBean> navigateList = new ArrayList<NavigateBean>();
		for (int i = 1; i <= pageCount; i++) {
			
			NavigateBean bean = new NavigateBean();
			bean.setTitle("第"+i+"页");
			if( i == 1){
				bean.setLinkUrl(prefix+".html");
			}else{
				bean.setLinkUrl(prefix+"_"+i+".html");
			}
			
			navigateList.add(bean);
		}
		
		return navigateList;
	}


}
